package com.assembly.system.service.impl;

import com.assembly.entities.RoleResources;
import com.assembly.system.mapper.RoleResourcesMapper;
import com.assembly.system.service.IRoleResourcesService;
import com.assembly.util.base.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 平台角色资源关联信息表 服务实现类
 * </p>
 *
 * @author 丽人平台-www.assembly.com
 * @since 2021-03-29
 */
@Service
public class RoleResourcesServiceImpl extends ServiceImpl<RoleResourcesMapper, RoleResources> implements IRoleResourcesService {
    /***
     * 根据角色ID查询 资源信息
     * @param roleId 角色ID
     * @return 返回资源ID集合
     */
    @Override
    public List<String> queryResourcesIdByRoleId(String roleId) {

        return baseMapper.queryResourcesIdByRoleId(roleId);
    }

    /***
     * 新增或修改角色权限
     * @param roleId  角色ID
     * @param resourcesId  资源ID
     * @return 返回结果
     */
    @Override
    @Transactional
    public Result saveOrUpdateResourcesAndRole(String roleId, List<String> resourcesId) {
        if (StringUtils.isEmpty(roleId)){
            return Result.error("角色名称不能为空");
        }
        QueryWrapper wrapper =new QueryWrapper();
        wrapper.eq("role_id",roleId);
        baseMapper.delete(wrapper);
        baseMapper.saveOrUpdateResourcesAndRole(roleId,resourcesId);
        return Result.ok();
    }


}
